<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数据导出</title>
    <link href="../css/mems-index.css" type="text/css" rel="stylesheet"/>
    <link rel="stylesheet" href="http://www.jq22.com/jquery/font-awesome.4.6.0.css">
    <link href="../css/foundation-datepicker.css" rel="stylesheet"/>
   
</head>



<!--header-->
<header id="header" class="header">
    <div class="header-info left"><i></i><a href="data-export.html">数据导出列表</a></div>
    <div class="right-user right">
         <div class="clear t" >             <div id="headusername" class="role"></div>             <div class="icon"></div>         </div>
        <div class="head-down">
            <ul><li><a href="edit-pwd.html">修改密码</a></li><li><a onclick="loginOut()">退出系统</a></li></ul></div><div>
    </div>
    </div>
</header>
<!--menu-->
<aside id="aside" class="aside">
    <div class="logo">运营平台</div>
    <ul class="menu" id="asidemenu">
    </ul>
</aside>

<!--content-->
<div class="content">
    <div class="page-inner" style="padding-top: 13px">
        <table class="mems-search">
            <tr>
                <td colspan="2">提出时间：<input id="dpd1"/>～<input id="dpd2"/></td>

            </tr>
            <tr>
                <td colspan="2">医院名称：<input id="name" /></td>
            </tr>
            <tr>
                <td >&emsp;&emsp;区域：<select id="district"><option>请选择</option></select>&emsp;<select style="width: 200px" id="privence"><option>请选择</option></select>&emsp;<select id="city"><option>请选择</option></select></td>
                <td align="left" ><button class="btn" id="checkout">查询</button></td>
            </tr>
            <tr>
                <td>&emsp;&emsp;内容：<select id="contentList"></select>&emsp;&emsp;维度：<select id="dimenList"></select></td>
                <td><a class="btn" onclick="exportExcel();" id="exportExcel" style="visibility: hidden">导出&nbsp;</a></td>
            </tr>
            <tr>
            </tr>
        </table>

        <div class="item-content">
            <div class="table-box">
                <table>
                    <thead>
                        <tr class="odd">
                            <th><input type="checkbox" id="selectAll"/>全选</th>
                            <th>ID</th>
                            <th>机构名称</th>
                            <th>大区</th>
                            <th>省份</th>
                            <th>城市</th>
                            <th>地区</th>
                    </tr>
                    </thead>
                    <tbody id="bodyList">

                    </tbody>
                </table>
            </div>
            <div class="pages clear">
                <div class="page-list clear" id="pageList">

                </div>
            </div>
        </div>
    </div>

</div>



<script src="../js/jquery.min.js"></script>
<script src="../js/jquery.cookie.js"></script>
<script src="../js/foundation-datepicker.min.js"></script>
<script src="../js/moment.js"></script>
<script src="../js/common.js"></script>
<script>
    var roleid = $.cookie('userrole');
    if(roleid ==3 || roleid == 9){
        $('#exportExcel').css({'visibility':'visible'})
    }
    var globalTotalIds = []

    function getData(p,checPara,sessionPara){
        var PerPage = 20;
        var url  = _ip+'/excel/list?page='+p+'&size='+PerPage;
        var data = {};
        if(checPara){
            var para = {
                "name": $('#name').val() || null,
                "content": $('#contentList').val() || null,
                "dim":$('#dimenList').val() || null,
                "beginDate": $('#dpd1').val()?$('#dpd1').val() + ' 00:00:00' : null,
                "endDate": $('#dpd2').val()?$('#dpd2').val() + ' 00:00:00' : null,
                "district": $('#district').find('option:selected').text()=='请选择'?null:$('#district').val(),
                "province": $('#privence').find('option:selected').text()=='请选择'?null : $('#privence').val(),
                "city": $('#city').find('option:selected').text()=='请选择'?null : $('#city').val()
            }
            data = para
        }
        if(sessionPara){
            data = sessionPara;
        }
        $.ajax({
            url:url,
            type : "POST",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            dataType: 'json',
            contentType:'application/json',
            data:JSON.stringify(data),
            success: function(json) {
                globalTotalIds.splice(0,globalTotalIds.length)
                if(json.message === 'LOGIN') {
                    window.location.href = 'user-login.html';
                    return false;
                }
                var storage = {
                    p:p,
                    para:data
                }
                sessionStorage.setItem('bug', JSON.stringify(storage));

                if(!json.data || json.data.length <= 0){
                    $('#bodyList').html('<tr><td colspan="8">暂无数据！</td></tr>');
                    $('#pageList').html('');
                    return false;
                }
                var _data     = [];
                var _pager    = [];
                var totalPage = json.totlePage;
                var prev      = p-1;
                if(prev < 1)prev = 1;
                var next = p + 1;
                if(next > totalPage)next = totalPage;
                for(var k = 0, kk = json.data.length; k < kk; k++){
                    var msg = json.data[k];

                    _data.push('<tr>');
                    _data.push('<td><input type="checkbox" value="'+msg.id+'"  '+(globalTotalIds.indexOf(msg.id)==-1?'':'checked') +'/></td>');
                    _data.push('<td>'+msg.id+'</td>');
                    _data.push('<td>'+(msg.name || '')+'</td>');
                    _data.push('<td>'+(msg.district || '')+'</td>');
                    _data.push('<td>'+(msg.province || '')+'</td>');
                    _data.push('<td>'+(msg.city|| '')+'</td>');
                    _data.push('<td>'+(msg.area || '')+'</td>');
                }
                _pager.push('<span>共：'+ totalPage +'页</span>');
                _pager.push('<span>您正在第：'+p+'页</span>')
                _pager.push('<a href="javascript:;" class="page-pre" onclick="getData('+prev+',true'+');return !1;">上一页</a>');
                _pager.push('<a href="javascript:;" class="page-next" onclick="getData('+next+',true'+');return !1;">下一页</a>');
                _pager.push('<span>前往第</span>');
                _pager.push('<input type="text" id="goToPage"/>');
                _pager.push('<span>页</span>');
                _pager.push('<button id="goToPageBtn">GO</button>');
                $('#bodyList').html(_data.join(""));
                $('#pageList').html(_pager.join(""));
                $('#goToPageBtn').on('click',function () {
                    var v = $('#goToPage').val();
                    if(!/^[1-9]\d*$/.test(v)){
                        return false;
                    }
                    if(v>1 && v<totalPage){
                        getData(v,true);
                    }
                    if(v <= 1){
                        getData(1,true);
                        return false;
                    }
                    if(v >= totalPage){
                        getData(totalPage,true);
                        return false;
                    }
                })

                selectSingle()
            }
        })
    }
    $('#checkout').on('click',function(){
        var contentList = $('#contentList').find('option:selected').text()=='请选择'?false:$('#contentList').val()
        var dimenList = $('#dimenList').find('option:selected').text()=='请选择'?false:$('#dimenList').val()
        if(!contentList){
            alert('请选择内容！')
            return
        }
        if(!dimenList){
            alert('请选择维度！')
            return
        }
        getData(1,true);
    })

    function init() {
        _API.dynamicDistrick('district','privence','city');
        var sessionPage = JSON.parse(sessionStorage.getItem('bug')) || null;
        if(sessionPage){
            var p = sessionPage.p;
            var para = sessionPage.para;
            getData(p,false,para);
            $('#name').val(sessionPage.para.name);
            $('#creater').val(sessionPage.para.creater);
            $('#dpd1').val(sessionPage.para.startTime?sessionPage.para.startTime.split(' ')[0]:'');
            $('#dpd2').val(sessionPage.para.endTime?sessionPage.para.endTime.split(' ')[0]:'');
            _API.getExcelTheme('contentList',sessionPage.para.product,true).getExcelDimen('dimenList',sessionPage.para.status,true);
        }else{
            getData(1);
            _API.getExcelTheme('contentList',false,true).getExcelDimen('dimenList',0,true);
        }
    }
    init()

    function selectAll() {
        $('#selectAll').click(function () {
            var checked = $(this).prop('checked')
            var allCheckBox = $('#bodyList').find('input[type=checkbox]')
            if(checked){
                allCheckBox.prop({'checked':true})
                allCheckBox.each(function () {
                    var _val = +$(this).val()
                    if(globalTotalIds.indexOf(_val) == -1){
                        globalTotalIds.push(_val)
                    }
                })
            }else{
                allCheckBox.prop({'checked':false})
                allCheckBox.each(function () {
                    var _val = $(this).val()
                    if(globalTotalIds.indexOf(_val) > -1){
                        globalTotalIds.remove(_val)
                    }
                })
            }
            console.log(globalTotalIds)
        })
    }
    selectAll()

    function selectSingle(){
        var allCheckBox = $('#bodyList').find('input[type=checkbox]')
        allCheckBox.on('click',function () {
            var checked = $(this).prop('checked')
            var _val = +$(this).val()
            if(checked && globalTotalIds.indexOf(_val) == -1){
                globalTotalIds.push(_val)
            }else{
                globalTotalIds.remove(_val)
            }
            console.log(globalTotalIds)
        })
    }
    function exportExcel() {
        var url = _ip+'/excel/exportParams'
        if(!globalTotalIds.length){
            alert('为选择任何医院！')
            return
        }
        var contentList = $('#contentList').val() || null
        if(contentList == null){
            alert('请选择内容！')
            return
        }
        var dimenList = $('#dimenList').val() || null
        if(dimenList == null){
            alert('请选择维度！')
            return
        }
        var data = {
                name:$('#name').val() || null,
                content: contentList,
                dim: dimenList,
                beginDate:$('#dpd1').val() || null,
                endDate:$('#dpd2').val() || null,
                district: $('#district').find('option:selected').text()=='请选择'?null:$('#district').val(),
                province: $('#privence').find('option:selected').text()=='请选择'?null : $('#privence').val(),
                city: $('#city').find('option:selected').text()=='请选择'?null : $('#city').val(),
                stringids: globalTotalIds.join(',') || null
            }
        $.ajax({
            url:url,
            type : "POST",
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            dataType: 'json',
            contentType:'application/json',
            data:JSON.stringify(data),
            success: function(json) {
                if(json.message === 'LOGIN') {
                    window.location.href = 'user-login.html';
                    return false;
                }
                if(json.success){
                    location.href = _ip+'/excel/export';
                    $$.closeLayer()
                }else{
                    alert(json.msg || '添加失败，稍后重试！')
                }

            }
        })
    }




    //构造删除数组中某一个元素的函数
    Array.prototype.remove = function(val) {
        var index = this.indexOf(val);
        if (index > -1) {
            this.splice(index, 1);
        }
    };

    var dateMax = moment().subtract(1,'d').format('YYYY-MM-DD')
    var nowTemp = new Date();
    var now = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0);
    var checkin = $('#dpd1').fdatepicker({
        onRender: function (date) {
            return date.valueOf() < now.valueOf() ? '' : '';
        },
        endDate: dateMax,
        // pickTime: true,
        format: 'yyyy-mm-dd'
    }).on('changeDate', function (ev) {
        if (ev.date.valueOf() > checkout.date.valueOf()) {
            var newDate = new Date(ev.date)
            newDate.setDate(newDate.getDate() + 1);
            checkout.update(newDate);
        }
        checkin.hide();
        $('#dpd2')[0].focus();
    }).data('datepicker');
    var checkout = $('#dpd2').fdatepicker({
        onRender: function (date) {
            return date.valueOf() <= checkin.date.valueOf() ? 'disabled' : '';
        },
        // pickTime: true,
        endDate: new Date(),
        format: 'yyyy-mm-dd'
    }).on('changeDate', function (ev) {
        checkout.hide();
    }).data('datepicker');


</script>
</body>
</html>